var goodsMonitorCtrl  = new XFApp.client();
var stats_approves ;
var stats_currencys ;
var stats_goodsStatus ;
XFApp.client.prototype.goodsStats = {
	successCode : "10000",
	page : null,
	hisPage : null,
	hisId : 0,
	cusPage : null,
	riskLevelId:null,
	urlItems : {
		queryByPageURL : basePath+"/goodsMonitor/queryPage",
		queryCustomerByPageURL : basePath + "/goodsMonitor/queryCusPage",
		setRiskLevelURL : basePath + "/goodsMonitor/setRisk",
		approveURL : basePath+"/goodsMonitor/allow",
		refuseURL : basePath+"/goodsMonitor/refuse",
		downLoadReportURL:basePath+"/goodsMonitor/downloadRepost"
	},
	
	queryPage : function(){
		_this = this;
		if(!!_this.page){
			_this.page.page("remote",0,$('#searchForm').serialize());
		}else{
			_this.page = $("#page").page({
				pageSize : 10,
				remote: {
					url: _this.urlItems.queryByPageURL,
			        params: $('#searchForm').serialize(), 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		var pageResult = data.result;
			        		if(pageResult.itemList.length>0){
			        			for(var index in pageResult.itemList){
			        				var element = pageResult.itemList[index];
			        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
			        			}
			        			$("#page_grid").html($("#dataTmp").render(pageResult.itemList));
			        		}else{
			        			$("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
			        		}
			        	}else{
			        		webDialogUtil.simpleAlert(data.msg, "温馨提示"); 
			        	}
			        },
			        error : function(){
			        	webDialogUtil.simpleAlert("系统出现异常,请联系开发人员", "温馨提示"); 
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页,{total}项'
			});
		}
		$("#check_all").prop("checked","");
	},
	queryCusPage : function(){
		_this = this;
		var params = {
			customerCode : $("#customercodeFilter").val(),
			customerName: $("#customernameFilter").val()
		}
		
		if(!!_this.cusPage){
			_this.cusPage.page("remote",0,params);
		}else{
			_this.cusPage = $("#customer_page").page({
				pageSize : 5,
				remote: {
					url: _this.urlItems.queryCustomerByPageURL,
			        params: params, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		var pageResult = data.result;
			        		$("#customer_data_grid").html($("#cusDataTmp").render(pageResult.itemList));
			        	}else{
			        		webDialogUtil.simpleAlert(data.msg, "温馨提示"); 
			        	}
			        },
			        error : function(){
			        	webDialogUtil.simpleAlert("数据查询异常，请联系开发人员", "温馨提示"); 
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页'
			});
		}
	},
	setRiskLevel:function(){
		var riskLevel= $('input[name="riskLevelSelect"]:checked').val();
		var params = {
			id:_this.riskLevelId,
			riskLevel:riskLevel
		};
		$.ajax({
			url : _this.urlItems.setRiskLevelURL,
			dataType : 'json',
			data  : params,
			type : 'post',
			success : function(data) {
				if(data.code!=10000){
					webDialogUtil.simpleAlert(data.msg, "温馨提示");
				}else{
					$("#riskLevel_modal").modal('hide');
					_this.queryPage();
				}
			}
		});
	},
	approve : function(){
		_this = this;
		var ids = [];
		$("[id^=checkbox_]").each(function(){
			if(!!$(this).prop("checked")){
				ids.push($(this).val());
			}
		});
		if(ids.length == 0){
			webDialogUtil.simpleAlert("请选择需要审批通过的选项", "温馨提示");
			return;
		}
		var parmas = {
				ids:ids,
		}
		webDialogUtil.simpleConfrimAlert("是否确认审批通过", function(){
			$.ajax({
				url : _this.urlItems.approveURL,
				dataType : 'json',
				traditional:true,
				data  : parmas,
				type : 'post',
				success : function(data) {
					if(data.code!=10000){
						webDialogUtil.simpleAlert(data.msg, "温馨提示");
					}else{
						webDialogUtil.simpleAlert("处理成功", "温馨提示");
						_this.queryPage();
					}
				}
			});
		}, null, "温馨提示");
	},
	refuse : function(){
		_this = this;
		var ids = [];
		$("[id^=checkbox_]").each(function(){
			if(!!$(this).prop("checked")){
				ids.push($(this).val());
			}
		});
		if(ids.length == 0){
			webDialogUtil.simpleAlert("请选择需要审批拒绝的选项", "温馨提示");
			return;
		}
		
		webDialogUtil.simpleConfrimAlert("是否确认审批拒绝", function(){
			$.ajax({
				url : _this.urlItems.refuseURL,
				dataType : 'json',
				traditional:true,
				data  : {ids:ids},
				type : 'post',
				success : function(data) {
					if(data.code!=10000){
						webDialogUtil.simpleAlert(data.msg, "温馨提示");
					}else{
						webDialogUtil.simpleAlert("处理成功", "温馨提示");
						_this.queryPage();
					}
				}
			});
		}, null, "温馨提示");
	},
	clk_check : function(){
		if(!!$("#check_all").prop("checked")){
			$("[id^=checkbox_]").prop("checked","checked");
		}else{
			$("[id^=checkbox_]").prop("checked","");
		}
	},
	riskLevelModal : function(id){
		this.riskLevelId = id;
		$("#riskLevel_modal").modal('show'); 
		$(".risk_checked").prop("checked","checked");
	},
	selCustomer : function(value){
		$("#customercodeFuzzy").val(value);
		$("#choose_customer_modal").modal('hide'); 
	},
	queryHistory : function(id){
		_this.queryHisPage(id);
		$("#choose_history_modal").modal('show'); 
	},
	event : function(){
		_this = this;
		$("#goods_search").on("click",function(){
			_this.queryPage();
		});
		$("#customercode_choose").on("click",function(){
			$("#choose_customer_modal").modal();
		});
		$("#custom_serach").on("click",function(){
			_this.queryCusPage();
		});
		$("#check_all").on("click",function(){
			_this.clk_check();
		});
		$("#custom_reset").on("click",function(){
			$("input[name='customercodeFilter']").val("");
			$("input[name='customernameFilter']").val("");
		});
		$("#riskLevelBtn").on("click",function(){
			_this.setRiskLevel();
		});
		$("#goods_pass_btn").on("click",function(){
			_this.approve();
		});
		$("#goods_refuse_btn").on("click",function(){
			_this.refuse();
		});
	},
	downLoadReport:function(id){
		var params = [];
		var param = {
				name:"id",
				value:id
		}
		params.push(param);
		$.ajaxFileUpload({
			url : _this.urlItems.downLoadReportURL,
			secureuri:false,
			dataType: 'json',
			data : params,
			success: function (data, status) {
				if(data.code!=10000){
					webDialogUtil.simpleAlert(data.msg, "温馨提示");
				}
			},
			error: function (data, status, e) {
				webDialogUtil.simpleAlert("下载异常", "温馨提示");
			}
		});
	},
	initStatus:function(){
		stats_goodsStatus = dictCtrl.dictionary.translate("goodsDocketStatus");
		stats_currencys = dictCtrl.dictionary.translate("currency");
		$.views.converters({
			fgoodsStatus : function(value) {
				if(!value){
					return "";
				}
				for(index in stats_goodsStatus){
					var goodsStatus = stats_goodsStatus[index];
					if(goodsStatus.dictCode == value){
						return goodsStatus.dictName;
					}
				}
				return  "未知类型("+value+")";
			},
			fmoneyUnit : function(value){
				if(!value){
					return "";
				}
				for(index in stats_currencys){
					var currencys = stats_currencys[index];
					if(currencys.dictCode == value){
						return currencys.dictName;
					}
				}
				return  "未知类型("+value+")";
			}
		});
	}
};
$(function(){
	goodsMonitorCtrl.goodsStats.initStatus();
	goodsMonitorCtrl.goodsStats.event();
	goodsMonitorCtrl.goodsStats.queryPage();
});

/* center modal */
function centerModals(){
    $('.modal').each(function(i){
        var $clone = $(this).clone().css('display', 'block').appendTo('body');    var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 2);
        top = top > 0 ? top : 0;
        $clone.remove();
        $(this).find('.modal-content').css("margin-top", top);
    });
}
$('.modal').on('show.bs.modal', centerModals);
$(window).on('resize', centerModals);